package com.app.micaihu.view.user.userinfo.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ListView;

import androidx.annotation.Nullable;

import com.app.micaihu.R;
import com.app.micaihu.base.BaseOldFragment;
import com.app.micaihu.custom.view.LoadMoreViewWithEmpty;
import com.app.widget.LoadView;
import com.app.utils.scrollablelayout.ScrollableHelper;

import java.util.ArrayList;

/**
 * Created by Guo on 2017/4/17.
 * <p>
 * <p>
 * <p>
 * 普通listview 带加载更多功能的 fragment 的基类(个人主页 应用)
 */

public abstract class HeaderViewBaseFragment<T> extends BaseOldFragment implements ScrollableHelper.ScrollableContainer, AbsListView.OnScrollListener, View.OnClickListener {


    protected ArrayList<T> mDatalist;

    protected ListView mListView;

    protected boolean isRefreshing;//是否正在加载
    protected LoadView lvLoading;//空view

    protected LoadMoreViewWithEmpty loadMoreView;//显示更多

    protected int page = 1;//数据页码
    protected View view;

    public HeaderViewBaseFragment() {
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.listview_fragment, container, false);
        mListView = (ListView) view.findViewById(R.id.listview);
        mListView.setOnScrollListener(this);


        lvLoading = (LoadView) view.findViewById(R.id.lv_loadview);
        lvLoading.setErrorPageClickListener(this);


        if (loadMoreView == null) {
            loadMoreView = new LoadMoreViewWithEmpty(getActivity());
            loadMoreView.setErrorPageOnClickListener(this);
            mListView.addFooterView(loadMoreView);

        }

        return view;
    }


    /**
     * 设置刷新中
     *
     * @param msg
     */
    public void onRefreshing(String msg) {


        if (this.loadMoreView != null && this.lvLoading != null) {
            if (mDatalist == null) {
                mDatalist = new ArrayList<>();
            }
            if (!mDatalist.isEmpty()) {
                this.loadMoreView.showLoading(msg);
                this.isRefreshing = true;
            } else {
                mListView.setEmptyView(lvLoading);
                lvLoading.showLoadPage("");
            }

        }
    }


    /**
     * 刷新完成
     */
    public void onComplete(int type, String msg) {
        if (this.loadMoreView != null) {
            switch (type) {
                case 1://加载成功
                    this.loadMoreView.showSuccess();

                    break;
                case 2://加载错误
                    this.loadMoreView.showErrorPage(msg);
                    break;
                case 3://空数据
                    this.loadMoreView.showEmptyPage(msg);
                    break;
            }

            this.isRefreshing = false;
        }
    }


    @Override
    public View getScrollableView() {
        return mListView;
    }


    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {
        if (scrollState == AbsListView.OnScrollListener.SCROLL_STATE_IDLE) {
//            LogUtils.e("gzq", "lastVisi = "+view.getLastVisiblePosition() + "    count = "+view.getCount()+"     mDataList = "+mDatalist.size());

            if (view.getLastVisiblePosition() >= (view.getCount() - 1)) {
                if (mDatalist == null || mDatalist.isEmpty()) return;
                loadMore();
            }
        }
    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

    }


    @Override
    public void onClick(View v) {
        if (v == null) {
            return;
        }
        //错误点击

        switch (v.getId()) {
            case R.id.error_page:// 重新加载页面数据(LoadView2)
                if (isRefreshing)
                    break;
                loadData(true);
                break;
            case R.id.load_error_page://加载更多(footerView)
                loadData(false);
                break;
            default:

                break;
        }
    }


    /**
     * 请求数据
     *
     * @param isRefresh
     */
    public abstract void loadData(boolean isRefresh);


    /**
     * 加载更多
     */
    public void loadMore() {
        if (isRefreshing) {
            return;
        }
        page++;
        loadData(false);
    }

}
